package com.yycx.module.bus.provider.handler.haiying.tasktodo.askamount;

import com.yycx.bpm.provider.listener.FlowPageInterceptor;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.*;
import com.yycx.module.file.provider.service.SysFileService;
import com.yycx.module.system.client.entity.SysBank;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysBankService;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.flowable.common.engine.api.query.Query;
import org.flowable.task.api.TaskInfoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 认款审批我的待办
 *
 * @author zyf
 */
@Component
public class HyAskAmountTaskTab1Handler implements FlowPageInterceptor<TaskResponse> {

    @Autowired
    private BusPaymentService paymentService;

    @Autowired
    private BusDeptService deptService;


    @Autowired
    private SysBankService bankService;

    @Autowired
    private BusSalemanService salemanService;

    @Override
    public void prepare(Query query, EntityMap params) {
        TaskInfoQuery taskQuery = (TaskInfoQuery) query;
        String amountType = params.get("amountType");
        String paymentNo = params.get("paymentNo");
        BusPayment payment = paymentService.getByProperty("paymentNo", paymentNo);
        if (FlymeUtils.isNotEmpty(payment)) {
            taskQuery.processInstanceBusinessKey(payment.getPaymentId().toString());
        } else {
            taskQuery.processInstanceBusinessKey(paymentNo);
        }
        if (FlymeUtils.isNotEmpty(amountType)) {
            taskQuery.processVariableValueEquals("amountType", amountType);
        }
        //排除发起人节点
        taskQuery.taskDefinitionKeyLike("Activity_%");
        taskQuery.taskTenantId("flp").orderByTaskCreateTime().desc();
        taskQuery.processVariableValueEquals("companyId", OpenHelper.getCompanyId());
        taskQuery.processDefinitionKey("process_return_flp");
    }

    @Override
    public List<EntityMap> complete(Query query, List<TaskResponse> data, EntityMap params) {
        List<EntityMap> list = new ArrayList<>();
        String companyName = OpenHelper.getAttr("companyName", String.class);
        if (FlymeUtils.isNotEmpty(data)) {
            for (TaskResponse taskResponse : data) {
                EntityMap map = new EntityMap();
                String paymentId = taskResponse.getBusinessKey();
                if (FlymeUtils.isNotEmpty(paymentId)) {
                    BusPayment payment = paymentService.getById(paymentId);
                    if (FlymeUtils.isNotEmpty(payment)) {
                        Long deptId = payment.getDeptId();
                        if (FlymeUtils.isNotEmpty(deptId)) {
                            BusDept dept = deptService.getById(deptId);
                            map.put("deptName", dept.getDeptName());
                            map.put("deptType", dept.getDeptType());
                        }
                        //单据类型
                        map.put("companyName", companyName);
                        String amountType = payment.getAmountType();
                        getHuikuanMap(payment, map);
                        map.put("paymentNo", payment.getPaymentNo());
                        map.put("paymentId", payment.getPaymentId());
                        map.put("amountType", amountType);
                        map.put("remark", payment.getRemark());
                        map.put("amount", payment.getAmount());
                        map.put("businessArea", payment.getBusinessArea());
                        map.put("nickName", payment.getNickName());
                        map.put("payUserName", payment.getPayUserName());
                        map.put("payBankNo", payment.getPayBankNo());
                        map.put("createTime", payment.getCreateTime());
                        map.put("amountDate", DateUtils.formatDate(payment.getAmountDate(), "yyyy-MM-dd"));
                        map.put("paymentState", payment.getPaymentState());
                        map.put("orderId", payment.getOrderId());
                    }
                    map.put("assignee", taskResponse.getAssignee());
                    map.put("processInstanceId", taskResponse.getProcessInstanceId());
                    map.put("taskName", taskResponse.getName());
                    map.put("taskId", taskResponse.getId());
                    map.put("endTime", taskResponse.getEndTime());
                    list.add(map);
                }
            }
        }
        return list;
    }

    /**
     * 查询回款信息
     *
     * @param payment
     * @param map
     */
    private void getHuikuanMap(BusPayment payment, EntityMap map) {
        //收款银行ID
        Long bankId = payment.getBankId();
        SysBank bank = bankService.getById(bankId);
        if (FlymeUtils.isNotEmpty(bank)) {
            map.put("bankName", bank.getBankName());
            map.put("bankId", bank.getBankId());
        }
        Long saleManId = payment.getSaleManId();
        if (FlymeUtils.isNotEmpty(saleManId)) {
            BusSaleman saleman = salemanService.getById(saleManId);
            map.put("businessCode", saleman.getSaleManBusinessNo());
            map.put("saleManName", saleman.getSaleManName());
        }
    }


}
